package com.hengtuo.kaiyuanzhongguo;

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;


import com.hengtuo.fragment.MinenearFragment;
import com.hengtuo.fragment.NearFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 活动    近期活动 http://www.oschina.net/action/api/event_list?pageIndex=1&uid=0&pageSize=10
 */
public class ExerciseActivity extends FragmentActivity implements View.OnClickListener {

    private ImageView exerciseback;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<Fragment> list;
    private FragmentPagerAdapter adapter;
    private String[] title = {"近期活动", "我的活动"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.exercise_activity);

        initialize();

        titlebar();

    }

    private void titlebar() {

        addFragment();
        viewPager.setAdapter(adapter);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        //设置tabLayout 标题来自mAdapter.getPageTitle
        tabLayout.setTabsFromPagerAdapter(adapter);
        //和ViewPager实现联动滑动
        TabLayout.TabLayoutOnPageChangeListener listener = new TabLayout.TabLayoutOnPageChangeListener(tabLayout);
        //这里是addOnPageChangeListener,也就是说，你还可以add N个Listener而不会被覆盖掉。
        viewPager.addOnPageChangeListener(listener);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void addFragment() {

        list = new ArrayList<>();
        NearFragment nearFragment = null;
        MinenearFragment mineFragment = null;
        nearFragment = new NearFragment();
        mineFragment = new MinenearFragment();
        list.add(nearFragment);
        list.add(mineFragment);

        //此方法用来显示tab上的名字

        adapter = new FragmentPagerAdapter(ExerciseActivity.this.getSupportFragmentManager()) {

            //此方法用来显示tab上的名字
            @Override
            public CharSequence getPageTitle(int position) {

                return title[position];

            }

            @Override
            public Fragment getItem(int i) {
                return list.get(i);
            }

            @Override
            public int getCount() {
                return list.size();
            }

        };

    }


    private void initialize() {
        exerciseback = (ImageView) findViewById(R.id.exercise_back);
        exerciseback.setOnClickListener(this);
        tabLayout = (TabLayout) findViewById(R.id.exercise_tablayout);
        viewPager = (ViewPager) findViewById(R.id.exercise_viewpager);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.exercise_back:
                finish();
                break;
        }
    }
}
